{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import random\n",
    "import jsonlines\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_csv(file_path):\n",
    "    with open(file_path, \"r\") as f:\n",
    "        reader = csv.reader(f)\n",
    "        data = list(reader)\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Index(['Date', 'Open', 'High', 'Low', 'Close', 'Volume', 'Currency'], dtype='object')\n"
     ]
    }
   ],
   "source": [
    "file_path = \"/<YOUR_OWN_PATH>/ToolQA/data/raw_data/coffee/coffee_price.csv\"\n",
    "data = pd.read_csv(file_path)\n",
    "print(data.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_questions_per_template = 12\n",
    "question_id = 0\n",
    "questions = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'qid': 'easy-coffee-0003', 'question': 'What was the opening price of coffee on 2000-04-11?', 'answer': '96.0'}, {'qid': 'easy-coffee-0004', 'question': 'What was the opening price of coffee on 2007-04-09?', 'answer': '110.85'}, {'qid': 'easy-coffee-0005', 'question': 'What was the opening price of coffee on 2009-06-11?', 'answer': '130.8'}, {'qid': 'easy-coffee-0006', 'question': 'What was the opening price of coffee on 2005-07-18?', 'answer': '102.5'}, {'qid': 'easy-coffee-0007', 'question': 'What was the opening price of coffee on 2018-06-11?', 'answer': '117.25'}, {'qid': 'easy-coffee-0008', 'question': 'What was the opening price of coffee on 2021-04-09?', 'answer': '127.9'}, {'qid': 'easy-coffee-0009', 'question': 'What was the opening price of coffee on 2017-05-09?', 'answer': '132.8'}, {'qid': 'easy-coffee-0010', 'question': 'What was the opening price of coffee on 2013-12-26?', 'answer': '114.95'}, {'qid': 'easy-coffee-0011', 'question': 'What was the opening price of coffee on 2011-05-18?', 'answer': '268.0'}, {'qid': 'easy-coffee-0012', 'question': 'What was the opening price of coffee on 2018-03-06?', 'answer': '120.1'}]\n"
     ]
    }
   ],
   "source": [
    "for i in range(num_questions_per_template+1):\n",
    "    random_index = random.randint(0, len(data)-1)\n",
    "    row = data.iloc[random_index]\n",
    "    Date = row[\"Date\"]\n",
    "    Open = row[\"Open\"]\n",
    "    question = \"What was the opening price of coffee on {}?\".format(Date)\n",
    "    answer = str(Open)\n",
    "    questions.append({\"qid\":\"easy-coffee-{:0>4d}\".format(question_id), \"question\":question, \"answer\":answer})\n",
    "    question_id += 1\n",
    "print(questions[-10:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'qid': 'easy-coffee-0016', 'question': 'What was the lowest price of coffee on 2016-08-31?', 'answer': '144.7'}, {'qid': 'easy-coffee-0017', 'question': 'What was the lowest price of coffee on 2008-12-19?', 'answer': '110.25'}, {'qid': 'easy-coffee-0018', 'question': 'What was the lowest price of coffee on 2010-04-12?', 'answer': '133.05'}, {'qid': 'easy-coffee-0019', 'question': 'What was the lowest price of coffee on 2005-01-27?', 'answer': '103.05'}, {'qid': 'easy-coffee-0020', 'question': 'What was the lowest price of coffee on 2004-09-27?', 'answer': '83.3'}, {'qid': 'easy-coffee-0021', 'question': 'What was the lowest price of coffee on 2000-05-25?', 'answer': '98.5'}, {'qid': 'easy-coffee-0022', 'question': 'What was the lowest price of coffee on 2011-03-07?', 'answer': '275.0'}, {'qid': 'easy-coffee-0023', 'question': 'What was the lowest price of coffee on 2005-09-26?', 'answer': '88.15'}, {'qid': 'easy-coffee-0024', 'question': 'What was the lowest price of coffee on 2004-06-09?', 'answer': '80.65'}, {'qid': 'easy-coffee-0025', 'question': 'What was the lowest price of coffee on 2018-05-03?', 'answer': '122.45'}]\n"
     ]
    }
   ],
   "source": [
    "for i in range(num_questions_per_template+1):\n",
    "    random_index = random.randint(0, len(data)-1)\n",
    "    row = data.iloc[random_index]\n",
    "    Date = row[\"Date\"]\n",
    "    low = row[\"Low\"]\n",
    "    question = \"What was the lowest price of coffee on {}?\".format(Date)\n",
    "    answer = str(low)\n",
    "    questions.append({\"qid\":\"easy-coffee-{:0>4d}\".format(question_id), \"question\":question, \"answer\":answer})\n",
    "    question_id += 1\n",
    "print(questions[-10:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'qid': 'easy-coffee-0029', 'question': 'What was the highest price of coffee on 2002-11-18?', 'answer': '68.0'}, {'qid': 'easy-coffee-0030', 'question': 'What was the highest price of coffee on 2013-09-10?', 'answer': '114.6'}, {'qid': 'easy-coffee-0031', 'question': 'What was the highest price of coffee on 2022-06-24?', 'answer': '234.45'}, {'qid': 'easy-coffee-0032', 'question': 'What was the highest price of coffee on 2021-10-22?', 'answer': '206.4'}, {'qid': 'easy-coffee-0033', 'question': 'What was the highest price of coffee on 2011-12-16?', 'answer': '214.25'}, {'qid': 'easy-coffee-0034', 'question': 'What was the highest price of coffee on 2015-11-24?', 'answer': '123.0'}, {'qid': 'easy-coffee-0035', 'question': 'What was the highest price of coffee on 2021-02-10?', 'answer': '123.55'}, {'qid': 'easy-coffee-0036', 'question': 'What was the highest price of coffee on 2016-08-10?', 'answer': '141.75'}, {'qid': 'easy-coffee-0037', 'question': 'What was the highest price of coffee on 2001-04-23?', 'answer': '62.0'}, {'qid': 'easy-coffee-0038', 'question': 'What was the highest price of coffee on 2002-11-12?', 'answer': '65.0'}]\n"
     ]
    }
   ],
   "source": [
    "for i in range(num_questions_per_template+1):\n",
    "    random_index = random.randint(0, len(data)-1)\n",
    "    row = data.iloc[random_index]\n",
    "    Date = row[\"Date\"]\n",
    "    High = row[\"High\"]\n",
    "    question = \"What was the highest price of coffee on {}?\".format(Date)\n",
    "    answer = str(High)\n",
    "    questions.append({\"qid\":\"easy-coffee-{:0>4d}\".format(question_id), \"question\":question, \"answer\":answer})\n",
    "    question_id += 1\n",
    "print(questions[-10:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'qid': 'easy-coffee-0042', 'question': 'What was the closing price of coffee on 2009-09-09?', 'answer': '123.7'}, {'qid': 'easy-coffee-0043', 'question': 'What was the closing price of coffee on 2002-09-30?', 'answer': '54.5'}, {'qid': 'easy-coffee-0044', 'question': 'What was the closing price of coffee on 2001-01-22?', 'answer': '71.25'}, {'qid': 'easy-coffee-0045', 'question': 'What was the closing price of coffee on 2005-08-19?', 'answer': '96.1'}, {'qid': 'easy-coffee-0046', 'question': 'What was the closing price of coffee on 2002-07-03?', 'answer': '48.15'}, {'qid': 'easy-coffee-0047', 'question': 'What was the closing price of coffee on 2015-04-29?', 'answer': '138.35'}, {'qid': 'easy-coffee-0048', 'question': 'What was the closing price of coffee on 2018-06-21?', 'answer': '112.6'}, {'qid': 'easy-coffee-0049', 'question': 'What was the closing price of coffee on 2008-08-06?', 'answer': '137.9'}, {'qid': 'easy-coffee-0050', 'question': 'What was the closing price of coffee on 2001-03-15?', 'answer': '59.1'}, {'qid': 'easy-coffee-0051', 'question': 'What was the closing price of coffee on 2013-07-02?', 'answer': '124.1'}]\n"
     ]
    }
   ],
   "source": [
    "for i in range(num_questions_per_template+1):\n",
    "    random_index = random.randint(0, len(data)-1)\n",
    "    row = data.iloc[random_index]\n",
    "    Date = row[\"Date\"]\n",
    "    Close = row[\"Close\"]\n",
    "    question = \"What was the closing price of coffee on {}?\".format(Date)\n",
    "    answer = str(Close)\n",
    "    questions.append({\"qid\":\"easy-coffee-{:0>4d}\".format(question_id), \"question\":question, \"answer\":answer})\n",
    "    question_id += 1\n",
    "print(questions[-10:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'qid': 'easy-coffee-0054', 'question': 'What was the volume of coffee on 2012-04-30?', 'answer': '28'}, {'qid': 'easy-coffee-0055', 'question': 'What was the volume of coffee on 2002-05-20?', 'answer': '4'}, {'qid': 'easy-coffee-0056', 'question': 'What was the volume of coffee on 2009-03-09?', 'answer': '6'}, {'qid': 'easy-coffee-0057', 'question': 'What was the volume of coffee on 2004-12-16?', 'answer': '78'}, {'qid': 'easy-coffee-0058', 'question': 'What was the volume of coffee on 2013-09-11?', 'answer': '11'}, {'qid': 'easy-coffee-0059', 'question': 'What was the volume of coffee on 2013-06-21?', 'answer': '119'}, {'qid': 'easy-coffee-0060', 'question': 'What was the volume of coffee on 2013-04-08?', 'answer': '33418'}, {'qid': 'easy-coffee-0061', 'question': 'What was the volume of coffee on 2017-01-30?', 'answer': '14830'}, {'qid': 'easy-coffee-0062', 'question': 'What was the volume of coffee on 2021-04-12?', 'answer': '23948'}, {'qid': 'easy-coffee-0063', 'question': 'What was the volume of coffee on 2015-05-28?', 'answer': '11730'}]\n"
     ]
    }
   ],
   "source": [
    "for i in range(num_questions_per_template):\n",
    "    random_index = random.randint(0, len(data)-1)\n",
    "    row = data.iloc[random_index]\n",
    "    Date = row[\"Date\"]\n",
    "    Volume = row[\"Volume\"]\n",
    "    question = \"What was the volume of coffee on {}?\".format(Date)\n",
    "    answer = str(Volume)\n",
    "    questions.append({\"qid\":\"easy-coffee-{:0>4d}\".format(question_id), \"question\":question, \"answer\":answer})\n",
    "    question_id += 1\n",
    "print(questions[-10:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'qid': 'easy-coffee-0066', 'question': 'What was the percentage change in coffee price on 2015-04-27, based on the difference between the opening and closing prices?', 'answer': '-2.79%'}, {'qid': 'easy-coffee-0067', 'question': 'What was the percentage change in coffee price on 2015-05-15, based on the difference between the opening and closing prices?', 'answer': '-0.54%'}, {'qid': 'easy-coffee-0068', 'question': 'What was the percentage change in coffee price on 2014-12-21, based on the difference between the opening and closing prices?', 'answer': '0.0%'}, {'qid': 'easy-coffee-0069', 'question': 'What was the percentage change in coffee price on 2006-11-06, based on the difference between the opening and closing prices?', 'answer': '-0.26%'}, {'qid': 'easy-coffee-0070', 'question': 'What was the percentage change in coffee price on 2016-04-01, based on the difference between the opening and closing prices?', 'answer': '-0.08%'}, {'qid': 'easy-coffee-0071', 'question': 'What was the percentage change in coffee price on 2019-12-16, based on the difference between the opening and closing prices?', 'answer': '-0.04%'}, {'qid': 'easy-coffee-0072', 'question': 'What was the percentage change in coffee price on 2007-04-10, based on the difference between the opening and closing prices?', 'answer': '-0.36%'}, {'qid': 'easy-coffee-0073', 'question': 'What was the percentage change in coffee price on 2002-08-21, based on the difference between the opening and closing prices?', 'answer': '-0.11%'}, {'qid': 'easy-coffee-0074', 'question': 'What was the percentage change in coffee price on 2003-10-14, based on the difference between the opening and closing prices?', 'answer': '1.82%'}, {'qid': 'easy-coffee-0075', 'question': 'What was the percentage change in coffee price on 2004-10-08, based on the difference between the opening and closing prices?', 'answer': '-1.15%'}]\n"
     ]
    }
   ],
   "source": [
    "for i in range(num_questions_per_template):\n",
    "    random_index = random.randint(0, len(data)-1)\n",
    "    row = data.iloc[random_index]\n",
    "    Date = row[\"Date\"]\n",
    "    Open = row[\"Open\"]\n",
    "    Close = row[\"Close\"]\n",
    "    question = \"What was the percentage change in coffee price on {}, based on the difference between the opening and closing prices?\".format(Date)\n",
    "    answer = str(round((float(Close) - float(Open)) / float(Open) * 100, 2)) + '%'\n",
    "    questions.append({\"qid\":\"easy-coffee-{:0>4d}\".format(question_id), \"question\":question, \"answer\":answer})\n",
    "    question_id += 1\n",
    "print(questions[-10:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'qid': 'easy-coffee-0078', 'question': 'Was 2013-07-26 a bearish or bullish day for coffee price?', 'answer': 'bearish'}, {'qid': 'easy-coffee-0079', 'question': 'Was 2004-08-23 a bearish or bullish day for coffee price?', 'answer': 'bullish'}, {'qid': 'easy-coffee-0080', 'question': 'Was 2009-04-29 a bearish or bullish day for coffee price?', 'answer': 'bullish'}, {'qid': 'easy-coffee-0081', 'question': 'Was 2011-07-12 a bearish or bullish day for coffee price?', 'answer': 'bearish'}, {'qid': 'easy-coffee-0082', 'question': 'Was 2017-06-12 a bearish or bullish day for coffee price?', 'answer': 'bullish'}, {'qid': 'easy-coffee-0083', 'question': 'Was 2001-09-17 a bearish or bullish day for coffee price?', 'answer': 'bearish'}, {'qid': 'easy-coffee-0084', 'question': 'Was 2020-04-16 a bearish or bullish day for coffee price?', 'answer': 'bearish'}, {'qid': 'easy-coffee-0085', 'question': 'Was 2007-11-14 a bearish or bullish day for coffee price?', 'answer': 'bullish'}, {'qid': 'easy-coffee-0086', 'question': 'Was 2007-04-02 a bearish or bullish day for coffee price?', 'answer': 'bearish'}, {'qid': 'easy-coffee-0087', 'question': 'Was 2021-05-19 a bearish or bullish day for coffee price?', 'answer': 'bearish'}]\n"
     ]
    }
   ],
   "source": [
    "for i in range(num_questions_per_template):\n",
    "    random_index = random.randint(0, len(data)-1)\n",
    "    row = data.iloc[random_index]\n",
    "    Date = row[\"Date\"]\n",
    "    Open = row[\"Open\"]\n",
    "    Close = row[\"Close\"]\n",
    "    question = \"Was {} a bearish or bullish day for coffee price?\".format(Date)\n",
    "    if float(Close) > float(Open):\n",
    "        answer = \"bullish\"\n",
    "    else:\n",
    "        answer = \"bearish\"\n",
    "    questions.append({\"qid\":\"easy-coffee-{:0>4d}\".format(question_id), \"question\":question, \"answer\":answer})\n",
    "    question_id += 1\n",
    "print(questions[-10:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'qid': 'easy-coffee-0090', 'question': 'What was the range of coffee price on 2017-09-28, based on the difference between the high and low prices??', 'answer': '2.2'}, {'qid': 'easy-coffee-0091', 'question': 'What was the range of coffee price on 2020-07-07, based on the difference between the high and low prices??', 'answer': '3.35'}, {'qid': 'easy-coffee-0092', 'question': 'What was the range of coffee price on 2012-04-18, based on the difference between the high and low prices??', 'answer': '2.1'}, {'qid': 'easy-coffee-0093', 'question': 'What was the range of coffee price on 2018-05-07, based on the difference between the high and low prices??', 'answer': '1.1'}, {'qid': 'easy-coffee-0094', 'question': 'What was the range of coffee price on 2021-06-18, based on the difference between the high and low prices??', 'answer': '3.95'}, {'qid': 'easy-coffee-0095', 'question': 'What was the range of coffee price on 2007-05-04, based on the difference between the high and low prices??', 'answer': '0.0'}, {'qid': 'easy-coffee-0096', 'question': 'What was the range of coffee price on 2004-08-06, based on the difference between the high and low prices??', 'answer': '1.95'}, {'qid': 'easy-coffee-0097', 'question': 'What was the range of coffee price on 2007-10-22, based on the difference between the high and low prices??', 'answer': '3.85'}, {'qid': 'easy-coffee-0098', 'question': 'What was the range of coffee price on 2006-05-11, based on the difference between the high and low prices??', 'answer': '3.5'}, {'qid': 'easy-coffee-0099', 'question': 'What was the range of coffee price on 2020-09-22, based on the difference between the high and low prices??', 'answer': '4.45'}]\n"
     ]
    }
   ],
   "source": [
    "for i in range(num_questions_per_template):\n",
    "    random_index = random.randint(0, len(data)-1)\n",
    "    row = data.iloc[random_index]\n",
    "    Date = row[\"Date\"]\n",
    "    High = row[\"High\"]\n",
    "    Low = row[\"Low\"]\n",
    "    question = \"What was the range of coffee price on {}, based on the difference between the high and low prices??\".format(Date)\n",
    "    answer = str(round(float(High) - float(Low), 2))\n",
    "    questions.append({\"qid\":\"easy-coffee-{:0>4d}\".format(question_id), \"question\":question, \"answer\":answer})\n",
    "    question_id += 1\n",
    "print(questions[-10:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "with jsonlines.open('/<YOUR_OWN_PATH>/ToolQA/data/questions/easy/coffee-easy.jsonl', mode='w') as writer:\n",
    "    for row in questions:\n",
    "        writer.write(row)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
